package com.itheima.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.pojo.OrderReturnApply;
import com.itheima.mall.service.OrderReturnApplyService;
import com.itheima.mall.utils.OmsUpdateParam;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/returnApply")
public class OrderReturnApplyController {

    @Autowired
    private OrderReturnApplyService orderReturnApplyService;

    /**
     * 分页查询退货申请
     *
     * @param createTime      申请时间
     * @param handleMan       处理人员
     * @param handleTime      处理时间
     * @param id              服务单号
     * @param pageNum         页数
     * @param pageSize        页面大小
     * @param status          申请状态：0->待处理；1->退货中；2->已完成；3->已拒绝
     * @param receiverKeyword 收货人姓名/号码
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult> list(String createTime, String handleMan, String handleTime,
                                           Integer id, Integer pageNum, Integer pageSize, Integer status,
                                           String receiverKeyword) {
        //创建分页对象
        Page<OrderReturnApply> pageInfo = new Page<>(pageNum, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<OrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置分页条件
        lambdaQueryWrapper.like(createTime != null, OrderReturnApply::getCreateTime, createTime)
                .eq(handleMan != null, OrderReturnApply::getHandleMan, handleMan)
                .like(handleTime != null, OrderReturnApply::getHandleTime, handleTime)
                .eq(id != null, OrderReturnApply::getId, id)
                .eq(status != null, OrderReturnApply::getStatus, status)
                .orderByAsc(OrderReturnApply::getCreateTime);
        Page<OrderReturnApply> page = orderReturnApplyService.page(pageInfo, lambdaQueryWrapper);
        //创建返回参数
        PageResult init = PageResult.init(page);
        return ResponseEntity.ok(init);
    }


    /**
     * 获取退货申请详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderReturnApply> selectById(@PathVariable Integer id) {
        OrderReturnApply orderReturnApply = orderReturnApplyService.getById(id);
        return ResponseEntity.ok(orderReturnApply);
    }

    /**
     * 修改退货申请状态
     *
     * @param id
     * @param statusParam
     * @return
     */
    @PostMapping("update/status/{id}")
    public ResponseEntity updateStatus(@PathVariable Integer id, @RequestBody OmsUpdateParam statusParam) {
        OrderReturnApply orderReturnApply = orderReturnApplyService.getById(id);
        orderReturnApply.setStatus(statusParam.getStatus());
        orderReturnApplyService.updateById(orderReturnApply);
        return ResponseEntity.ok("修改退货申请状态");
    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestBody Long[] ids) {
        orderReturnApplyService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok("删除成功");
    }

}



